package com.example.myapplication_2.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication_2.Main_Activity;
import com.example.myapplication_2.R;
import com.example.myapplication_2.service.SymptomService;
import com.example.myapplication_2.service.serviceImpl.SymptomServiceImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class SymptomActivity extends Activity implements View.OnClickListener {

    /*四个标签上的删除按钮*/
    private Button bt_delete1;
    private Button bt_delete2;
    private Button bt_delete3;
    private Button bt_delete4;
    /*四个标签的布局容器*/
    private LinearLayout ll_change1;
    private LinearLayout ll_change2;
    private LinearLayout ll_change3;
    private LinearLayout ll_change4;
    /*确定按钮*/
    private Button bt_confirm;
    /*四个标签上的文本容器*/
    private TextView tv_symptom1;
    private TextView tv_symptom2;
    private TextView tv_symptom3;
    private TextView tv_symptom4;

    /*八个圆形按钮*/
    private Button bt_1;
    private Button bt_2;
    private Button bt_3;
    private Button bt_4;
    private Button bt_5;
    private Button bt_6;
    private Button bt_7;
    private Button bt_8;
    private Button tv_return;
    /*换一换按钮*/
    private Button bt_change;
    /*自动提示补全输入框*/
    private AutoCompleteTextView actv_select;

    /*搜索症状按钮*/
    private Button bt_select;

    /*八个圆形按钮的大小*/
    //按钮内文字大小
    private int bigWordSize = 22;
    private int smallWordSize = 13;

    //按钮大小
    private int bigButtonSize = 260;
    private int lowBigButtonSize = 220;
    private int topSmallButtonSize = 180;
    private int smallButtonSize = 120;

    SymptomService symptomService = new SymptomServiceImpl();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /* requestWindowFeature(Window.FEATURE_NO_TITLE);//隐藏标题*/
        setTheme(R.style.AppTheme_NoActionBar);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);//设置全屏
        setContentView(R.layout.activity_symptom);
        init();

        //点击返回上一页
        tv_return = (Button)findViewById(R.id.tv_return);
        tv_return.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                // 给bnt2添加点击响应事件
                Intent intent = new Intent(SymptomActivity.this, Main_Activity.class);
                //启动
                startActivity(intent);
            }
        });
    }

    @Override
    protected void onResume() {
/**
 * 设置为横屏
 */
        if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        }
        super.onResume();
    }


    /**
     * 初始化数据、组件及其监听
     */
    void init() {

        bt_delete1 = (Button) this.findViewById(R.id.bt_delete1);
        bt_delete2 = (Button) this.findViewById(R.id.bt_delete2);
        bt_delete3 = (Button) this.findViewById(R.id.bt_delete3);
        bt_delete4 = (Button) this.findViewById(R.id.bt_delete4);

        ll_change1 = (LinearLayout) this.findViewById(R.id.ll_change1);
        ll_change2 = (LinearLayout) this.findViewById(R.id.ll_change2);
        ll_change3 = (LinearLayout) this.findViewById(R.id.ll_change3);
        ll_change4 = (LinearLayout) this.findViewById(R.id.ll_change4);

        bt_confirm = (Button) this.findViewById(R.id.bt_confirm);

        tv_symptom1 = (TextView) this.findViewById(R.id.tv_symptom1);
        tv_symptom2 = (TextView) this.findViewById(R.id.tv_symptom2);
        tv_symptom3 = (TextView) this.findViewById(R.id.tv_symptom3);
        tv_symptom4 = (TextView) this.findViewById(R.id.tv_symptom4);

        bt_1 = (Button) this.findViewById(R.id.bt_1);
        bt_2 = (Button) this.findViewById(R.id.bt_2);
        bt_3 = (Button) this.findViewById(R.id.bt_3);
        bt_4 = (Button) this.findViewById(R.id.bt_4);
        bt_5 = (Button) this.findViewById(R.id.bt_5);
        bt_6 = (Button) this.findViewById(R.id.bt_6);
        bt_7 = (Button) this.findViewById(R.id.bt_7);
        bt_8 = (Button) this.findViewById(R.id.bt_8);

        tv_return = (Button) this.findViewById(R.id.tv_return);
        bt_change = (Button) this.findViewById(R.id.bt_change);
        actv_select = (AutoCompleteTextView) this.findViewById(R.id.actv_select);

        bt_select = (Button) this.findViewById(R.id.bt_select);

        bt_delete1.setOnClickListener(this);
        bt_delete2.setOnClickListener(this);
        bt_delete3.setOnClickListener(this);
        bt_delete4.setOnClickListener(this);

        bt_confirm.setOnClickListener(this);

        bt_1.setOnClickListener(this);
        bt_2.setOnClickListener(this);
        bt_3.setOnClickListener(this);
        bt_4.setOnClickListener(this);
        bt_5.setOnClickListener(this);
        bt_6.setOnClickListener(this);
        bt_7.setOnClickListener(this);
        bt_8.setOnClickListener(this);

        tv_return.setOnClickListener(this);

        bt_change.setOnClickListener(this);

        bt_select.setOnClickListener(this);

        getNewSymptom(null);//为八个圆圈添加症状信息

        /*为输入框配置自动补全下拉框适配器*/
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(SymptomActivity.
                this, android.R.layout.simple_dropdown_item_1line, getAllSymptom());
        actv_select.setAdapter(adapter);

    }


    /**
     * 按钮监听
     * @param view
     */
    @SuppressLint("ResourceAsColor")
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            //标签的删除
            case R.id.bt_delete1:
                ll_change1.setVisibility(View.GONE);
                    for(int i=1;i<=8;i++)
                    {
                        if(getButton(i).getVisibility()== View.VISIBLE&&getButton(i).getText().equals(tv_symptom1.getText())){
                            changetoFocus(getButton(i));
                        }
                    }
                break;

            case R.id.bt_delete2:
                ll_change2.setVisibility(View.GONE);
                    for(int i=1;i<=8;i++)
                    {
                        if(getButton(i).getVisibility()== View.VISIBLE&&getButton(i).getText().equals(tv_symptom2.getText())){
                            changetoFocus(getButton(i));
                        }
                    }

                break;

            case R.id.bt_delete3:
                ll_change3.setVisibility(View.GONE);
                    for(int i=1;i<=8;i++)
                    {
                        if(getButton(i).getVisibility()== View.VISIBLE&&getButton(i).getText().equals(tv_symptom3.getText())){
                            changetoFocus(getButton(i));
                        }
                    }
                break;

            case R.id.bt_delete4:

                    for(int i=1;i<=8;i++)
                    {
                        if(getButton(i).getVisibility()== View.VISIBLE&&getButton(i).getText().equals(tv_symptom4.getText())){
                            changetoFocus(getButton(i));
                        }
                    }

                ll_change4.setVisibility(View.GONE);
                break;
            case R.id.bt_confirm:
                List<String> symptomList = new ArrayList<>();
                symptomList = getUsedSymptom();//获取的症状集合
                if(getUsedSymptom().size()==0){
                    Toast.makeText(getApplicationContext(),"未选中任何症状", Toast.LENGTH_LONG).show();
                }else {
                    symptomService.confirmSymptom(symptomList);
                    String s = new String();
                    for(int i=0;i<symptomList.size()-1;i++){
                        s=s+symptomList.get(i)+"、";
                    }
                    s+=symptomList.get(symptomList.size()-1);
                    Toast.makeText(getApplicationContext(),"获取到的症状："+s, Toast.LENGTH_LONG).show();
                }
                break;


            //标签的添加
            case R.id.bt_1:
                String symptom1 = (String) bt_1.getText();
                if (addSymptom(symptom1)) {
                    changeToNoFocus(bt_1);
                }
                break;

            case R.id.bt_2:
                String symptom2 = (String) bt_2.getText();
                if (addSymptom(symptom2)) {
                    changeToNoFocus(bt_2);
                }
                break;

            case R.id.bt_3:
                String symptom3 = (String) bt_3.getText();
                if (addSymptom(symptom3)) {
                    changeToNoFocus(bt_3);
                }
                break;

            case R.id.bt_4:
                String symptom4 = (String) bt_4.getText();
                if (addSymptom(symptom4)) {
                    changeToNoFocus(bt_4);
                }
                break;

            case R.id.bt_5:
                String symptom5 = (String) bt_5.getText();
                if (addSymptom(symptom5)) {
                    changeToNoFocus(bt_5);
                }
                break;

            case R.id.bt_6:
                String symptom6 = (String) bt_6.getText();
                if (addSymptom(symptom6)) {
                    changeToNoFocus(bt_6);
                }
                break;

            case R.id.bt_7:
                String symptom7 = (String) bt_7.getText();
                if (addSymptom(symptom7)) {
                    changeToNoFocus(bt_7);
                }
                break;

            case R.id.bt_8:
                String symptom8 = (String) bt_8.getText();
                if (addSymptom(symptom8)) {
                    changeToNoFocus(bt_8);
                }
                break;

            case R.id.bt_change:
                //更换八个圈内的症状信息
                getNewSymptom(getUsedSymptom());
                break;

            case R.id.bt_select:
                List<String> findSymptom = new ArrayList<>();
                Log.e("怎么忽视","获取到的信息"+actv_select.getText());
                if((actv_select.getText()+"").equals("")||actv_select.getText().equals(null)||(actv_select.getText()+"").equals(" ")){
                    cleanButton();
                    Toast.makeText(getApplicationContext(), "症状不存在！", Toast.LENGTH_SHORT).show();
                }else{
                    findSymptom = symptomService.findSymptomByWord(actv_select.getText() + "");
                    addData(findSymptom);
                }
                break;
        }
    }

    /**
     * 获取已经选中的标签内容闭并返回
     * @return 获取到的标签内症状集合
     */
    List<String> getUsedSymptom() {
        List<String> symptomList = new ArrayList<>();
        if (ll_change1.getVisibility() == View.VISIBLE) {
            String symptom = (String) tv_symptom1.getText();
            symptomList.add(symptom);
        }
        if (ll_change2.getVisibility() == View.VISIBLE) {
            String symptom = (String) tv_symptom2.getText();
            symptomList.add(symptom);
        }
        if (ll_change3.getVisibility() == View.VISIBLE) {
            String symptom = (String) tv_symptom3.getText();
            symptomList.add(symptom);
        }
        if (ll_change4.getVisibility() == View.VISIBLE) {
            String symptom = (String) tv_symptom4.getText();
            symptomList.add(symptom);
        }
        return symptomList;
    }

    /**
     * 添加标签及其内容
     * @param symptom 将要添加的症状信息
     * @return 是否添加成功
     */
    boolean addSymptom(String symptom) {
        if (ll_change4.getVisibility() == View.VISIBLE && ll_change3.getVisibility() == View.VISIBLE
                && ll_change2.getVisibility() == View.VISIBLE && ll_change1.getVisibility() == View.VISIBLE) {
            Toast.makeText(getApplicationContext(), "最多改善4种症状！", Toast.LENGTH_SHORT).show();
            return false;
        } else if (ll_change1.getVisibility() == View.GONE) {
            ll_change1.setVisibility(View.VISIBLE);
            tv_symptom1.setText(symptom);
            changeLengthByString(symptom, ll_change1, tv_symptom1);
            return true;
        } else if (ll_change2.getVisibility() == View.GONE) {
            ll_change2.setVisibility(View.VISIBLE);
            tv_symptom2.setText(symptom);
            changeLengthByString(symptom, ll_change2, tv_symptom2);
            return true;
        } else if (ll_change3.getVisibility() == View.GONE) {
            ll_change3.setVisibility(View.VISIBLE);
            tv_symptom3.setText(symptom);
            changeLengthByString(symptom, ll_change3, tv_symptom3);
            return true;
        } else if (ll_change4.getVisibility() == View.GONE) {
            ll_change4.setVisibility(View.VISIBLE);
            tv_symptom4.setText(symptom);
            changeLengthByString(symptom, ll_change4, tv_symptom4);
            return true;
        } else {
            return false;
        }
    }

    /**
     * 根据字符串长度调整标签长度
     * @param s 症状信息字符串
     * @param ll 标签所在布局容器
     * @param tv 标签文本框
     */
    void changeLengthByString(String s, LinearLayout ll, TextView tv) {
        if (s.length() > 2) {
            android.view.ViewGroup.LayoutParams llPara = ll.getLayoutParams();
            android.view.ViewGroup.LayoutParams tvPara = tv.getLayoutParams();
            llPara.width = 240;
            tvPara.width = 180;
        }else {
            android.view.ViewGroup.LayoutParams llPara = ll.getLayoutParams();
            android.view.ViewGroup.LayoutParams tvPara = tv.getLayoutParams();
            llPara.width = 160;
            tvPara.width = 100;
        }
    }

    /**
     * 获取除usedSymptom以外的任意八个症状字符串
     * @param usedSymptom 标签内已有的症状信息集合
     */
    @SuppressLint("ResourceAsColor")
    void getNewSymptom(List<String> usedSymptom) {
        if (usedSymptom == null)
            usedSymptom = new ArrayList<>();
        List<String> resultList = symptomService.findSymptom(usedSymptom);
        addData(resultList);
    }

    /**
     * 将symptomList症状集合内容填到圆内
     * @param symptomList 需要填入的症状集合
     * @return 是否填入成功
     */
    boolean addData(List<String> symptomList) {

        cleanButton();//清除所有圆
        if (symptomList.size() == 0) {
            Toast.makeText(getApplicationContext(), "症状不存在！", Toast.LENGTH_SHORT).show();
            return false;
        }
        /*将不同大小的圆随机放在不同位置*/
        int[] a = null;
        if (symptomList.size() > 3) {
            a = new int[3];
            a = randomNumber(1, 3, 3);
        } else {
            a = new int[symptomList.size()];
            a = randomNumber(1, 3, symptomList.size());
        }
        setButtonSizeContent(getButton(a[0]), bigButtonSize, bigWordSize, symptomList.get(0));
        if (!isExit(symptomList.get(0))) {
            changetoFocus(getButton(a[0]));
        } else {
            changeToNoFocus(getButton(a[0]));
        }

        for (int i = 2; i <= symptomList.size() && i <= 3; i++) {
            setButtonSizeContent(getButton(a[i-1]), bigButtonSize, bigWordSize, symptomList.get(i - 1));
            if (!isExit(symptomList.get(i-1))) {
                changetoFocus(getButton(a[i - 1]));
            } else {
                changeToNoFocus(getButton(a[i - 1]));
            }
        }
        if (symptomList.size() > 3) {
            int[] b = new int[symptomList.size() - 3];
            b = randomNumber(4, 8, symptomList.size() - 3);
            for (int i = 4; i <= symptomList.size() && i <= 6; i++) {
                setButtonSizeContent(getButton(b[i - 4]), topSmallButtonSize, smallWordSize, symptomList.get(i - 1));
                if (!isExit(symptomList.get(i-4))) {
                    changetoFocus(getButton(b[i - 4]));
                } else {
                    changeToNoFocus(getButton(b[i - 4]));
                }
            }
            if (symptomList.size() > 6) {
                for (int i = 7; i <= symptomList.size() && i <= 8; i++) {
                    setButtonSizeContent(getButton(b[i - 4]), smallButtonSize, smallWordSize, symptomList.get(i - 1));
                    if (!isExit(symptomList.get(i-1))) {
                        changetoFocus(getButton(b[i - 4]));
                    } else {
                        changeToNoFocus(getButton(b[i - 4]));
                    }
                }
            }
        }
        return true;
    }

    /**
     * 将圆变为可点击状态
     * @param button 需要改变状态的圆按钮
     */
    void changetoFocus(Button button) {
        button.setTextColor(getResources().getColor(R.color.text_color));
        button.setBackground(getResources().getDrawable(R.drawable.circle_style));
        button.setEnabled(true);
        button.setVisibility(View.VISIBLE);
    }

    /**
     * 将圆变为不可点击状态
     * @param button 需要改变状态的圆按钮
     */
    void changeToNoFocus(Button button) {
        button.setTextColor(getResources().getColor(R.color.gray_text));
        button.setBackground(getResources().getDrawable(R.drawable.nofocus_circle_style));
        button.setEnabled(false);
        button.setVisibility(View.VISIBLE);
    }

    /**
     * 将所有圆按钮置为不可见
     */
    void cleanButton() {
        bt_1.setVisibility(View.GONE);
        bt_2.setVisibility(View.GONE);
        bt_3.setVisibility(View.GONE);
        bt_4.setVisibility(View.GONE);
        bt_5.setVisibility(View.GONE);
        bt_6.setVisibility(View.GONE);
        bt_7.setVisibility(View.GONE);
        bt_8.setVisibility(View.GONE);
    }

    /**
     * 设置圆按钮的大小及内容
     * @param button 需要设置的圆按钮
     * @param bSize 按钮大小
     * @param wSize 按钮内文字大小
     * @param content 按钮内文字内容
     */
    void setButtonSizeContent(Button button, int bSize, int wSize, String content) {

        android.view.ViewGroup.LayoutParams buttonPara = button.getLayoutParams();
        buttonPara.width = bSize;
        buttonPara.height = bSize;
        /*button.setWidth(bSize);
        button.setHeight(bSize);*/
        button.setTextSize(wSize);
        button.setText(content);

    }

    /**
     * 特定区间产生n个从min到max的不重复随机数
     * @param min 区间起始值
     * @param max 区间结束值
     * @param n 所需产生随机数个数
     * @return 生成的整型随机数数组
     */
    int[] randomNumber(int min, int max, int n) {

        //判断是否已经达到索要输出随机数的个数
        if (n > (max - min + 1) || max < min) {
            return null;
        }

        int[] result = new int[n]; //用于存放结果的数组

        int count = 0;
        while (count < n) {
            Random rand = new Random();
            int num = rand.nextInt((max - min) + 1) + min;
            // int num = (int)(Math.random()*(max-min))+min;
            boolean flag = true;
            for (int j = 0; j < count; j++) {
                if (num == result[j]) {
                    flag = false;
                    break;
                }
            }
            if (flag) {
                result[count] = num;
                count++;
            }
        }
        return result;
    }

    /**
     * 根据数字获取对应按钮
     * @param i
     * @return 数字对应按钮
     */
    Button getButton(int i) {
        if (i == 1)
            return bt_1;
        else if (i == 2)
            return bt_2;
        else if (i == 3)
            return bt_3;
        else if (i == 4)
            return bt_4;
        else if (i == 5)
            return bt_5;
        else if (i == 6)
            return bt_6;
        else if (i == 7)
            return bt_7;
        else if (i == 8)
            return bt_8;
        else
            return null;
    }

    /**
     *   获取所有症状
     * @return 所有症状集合
     */
    List<String> getAllSymptom() {

        List<String> allSymptom = new ArrayList<String>();
        allSymptom = symptomService.findAllSymptom();
        return allSymptom;

    }


    /**
     * 判断症状是否已存在标签内的
     * @param symptom 待判断症状
     * @return 是否已存在
     */
    boolean isExit(String symptom) {
        boolean result = false;
        if (ll_change1.getVisibility() == View.VISIBLE && tv_symptom1.getText().equals(symptom)) {
            result = true;
        } else if (ll_change2.getVisibility() == View.VISIBLE && tv_symptom2.getText().equals(symptom)) {
            result = true;
        } else if (ll_change3.getVisibility() == View.VISIBLE && tv_symptom3.getText().equals(symptom)) {
            result = true;
        } else if (ll_change4.getVisibility() == View.VISIBLE && tv_symptom4.getText().equals(symptom)) {
            result = true;
        }
        return result;
    }
}

